package com.sdsmdg.harjot.vectormaster.utilities;

import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

/**
 * Utils class for the lib
 */
public class Utils {
    /**
     * Parse the color code from string
     *
     * @param value color String value
     * @return int color valve
     */
    public static int getColorFromString(String value) {
        int color;
        if (value.length() == 4) {
            String s = "#"
                    + "0" + value.charAt(1)
                    + "0" + value.charAt(2)
                    + "0" + value.charAt(3);
            color = Color.getIntColor(s);
        } else {
            try {
                color = Color.getIntColor(value);
            } catch (IllegalArgumentException e) {
                return Color.TRANSPARENT.getValue();
            }
        }
        return color != 0 ? color : Color.TRANSPARENT.getValue();
    }

    /**
     * Get Path.FillType
     *
     * @param value value
     * @return Path.FillType
     */
    public static Path.FillType getFillTypeFromString(String value) {
        Path.FillType fillType = Path.FillType.WINDING_ORDER;
        if ("1".equals(value)) {
            fillType = Path.FillType.EVEN_ODD;
        }
        return fillType;
    }

    /**
     * Get Paint.StrokeCap
     *
     * @param value value
     * @return Paint.StrokeCap
     */
    public static Paint.StrokeCap getLineCapFromString(String value) {
        switch (value) {
            case "0":
                return Paint.StrokeCap.BUTT_CAP;
            case "1":
                return Paint.StrokeCap.ROUND_CAP;
            case "2":
                return Paint.StrokeCap.SQUARE_CAP;
            default:
                return Paint.StrokeCap.BUTT_CAP;
        }
    }

    /**
     * Get the Paint.Join from string vale
     *
     * @param value value
     * @return Paint.Join
     */
    public static Paint.Join getLineJoinFromString(String value) {
        switch (value) {
            case "0":
                return Paint.Join.MITER_JOIN;
            case "1":
                return Paint.Join.ROUND_JOIN;
            case "2":
                return Paint.Join.BEVEL_JOIN;
            default:
                return Paint.Join.MITER_JOIN;
        }
    }

    /**
     * Convert Alpha float value to int
     *
     * @param value float alpha
     * @return int alpha
     */
    public static int getAlphaFromFloat(float value) {
        int newValue = (int) (255 * value);
        return Math.min(255, newValue);
    }

    /**
     * Convert Alpha int value to float
     *
     * @param value int alpha
     * @return float alpha
     */
    public static float getAlphaFromInt(int value) {
        return (((float) value) / 255.0f);
    }

    /**
     * Convert vp to px
     *
     * @param context Context
     * @param vp      vp
     * @return px value
     */
    public static int vpToPx(Context context, int vp) {
        DisplayManager wm = DisplayManager.getInstance();
        return (int) (vp * wm.getDefaultDisplay(context).get().getAttributes().densityPixels);
    }

    /**
     * Convert px to dp
     *
     * @param context Context
     * @param px      Pixel
     * @return vp vale
     */
    public static int pxToVp(Context context, int px) {
        DisplayManager wm = DisplayManager.getInstance();
        return (int) (px / wm.getDefaultDisplay(context).get().getAttributes().densityPixels);
    }

    /**
     * Convert string dimension to float
     *
     * @param value string dimension
     * @return dimension
     */
    public static float getFloatFromDimensionString(String value) {
        if (value.contains("dip")) {
            return Float.parseFloat(value.substring(0, value.length() - 3));
        } else {
            return Float.parseFloat(value.substring(0, value.length() - 2));
        }
    }

    /**
     * Object equal check
     *
     * @param obj1 Object
     * @param obj2 Object
     * @return isEqual
     */
    public static boolean isEqual(Object obj1, Object obj2) {
        return obj1 == null && obj2 == null || !(obj1 == null || obj2 == null) && obj1.equals(obj2);
    }

}
